package bp.net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class RestoreFile {

	private int inputNodeCount;
	private int outputNodeCount;
	private int hiddenNodeCount;
	private List<List<Double>> outputWeights;
	private List<List<Double>> hiddenWeights;
	private String fileName;

	public RestoreFile(String fileName) {
		this.fileName = fileName;
	}

	public void save() {
		BufferedWriter writer = null;
		try {
			File file = new File(this.fileName);
			if (!file.exists())
				file.createNewFile();
			writer = new BufferedWriter(new FileWriter(file));
			writeCounts(writer);
			writeWeights(writer);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private void writeWeights(BufferedWriter writer) throws IOException {
		for (List<Double> weights : this.hiddenWeights) {
			writer.write(weights.toString() + "\n");
		}
		writer.write("output:\n");
		for (List<Double> weights : this.outputWeights) {
			writer.write(weights.toString() + "\n");
		}
	}

	private void writeCounts(BufferedWriter writer) throws IOException {
		StringBuilder counts = new StringBuilder().append(this.inputNodeCount)
				.append(",").append(this.hiddenNodeCount).append(",")
				.append(this.outputNodeCount);
		writer.write(counts.toString() + "\n");
	}

	public void readFile() {
		File file = new File(this.fileName);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			getCounts(reader);
			getWeights(reader);
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	private void getCounts(BufferedReader reader) throws IOException {
		String s = reader.readLine();
		String[] sCounts = s.split(",");
		this.inputNodeCount = Integer.parseInt(sCounts[0]);
		this.hiddenNodeCount = Integer.parseInt(sCounts[1]);
		this.outputNodeCount = Integer.parseInt(sCounts[2]);
	}

	private void getWeights(BufferedReader reader) throws IOException {
		this.hiddenWeights = new ArrayList<List<Double>>();
		String tmp = null;
		while (!(tmp = reader.readLine().trim()).equals("output:")) {
			List<Double> ws = new ArrayList<Double>();
			String[] sws = tmp.substring(1, tmp.length() - 1).split(", ");
			for (String w : sws) {
				ws.add(Double.parseDouble(w.trim()));
			}
			this.hiddenWeights.add(ws);
		}

		this.outputWeights = new ArrayList<List<Double>>();
		while ((tmp = reader.readLine()) != null) {
			List<Double> ws = new ArrayList<Double>();
			String[] sws = tmp.substring(1, tmp.length() - 1).split(", ");
			for (String w : sws) {
				ws.add(Double.parseDouble(w.trim()));
			}
			this.outputWeights.add(ws);
		}
	}

	public void setHiddenNodeCount(int hiddenNodeCount) {
		this.hiddenNodeCount = hiddenNodeCount;
	}

	public int getHiddenNodeCount() {
		return hiddenNodeCount;
	}

	public void setOutputNodeCount(int outputNodeCount) {
		this.outputNodeCount = outputNodeCount;
	}

	public int getOutputNodeCount() {
		return outputNodeCount;
	}

	public void setInputNodeCount(int inputNodeCount) {
		this.inputNodeCount = inputNodeCount;
	}

	public int getInputNodeCount() {
		return inputNodeCount;
	}

	public void setHiddenWeights(List<List<Double>> hiddenWeights) {
		this.hiddenWeights = hiddenWeights;
	}

	public List<List<Double>> getHiddenWeights() {
		return hiddenWeights;
	}

	public void setOutputWeights(List<List<Double>> outputWeights) {
		this.outputWeights = outputWeights;
	}

	public List<List<Double>> getOutputWeights() {
		return outputWeights;
	}

}
